import importAll from '@base/utils/importAll'
import pageRouter from '../router/pageRouter'

const filterRouter = (router) => {
  return new Promise(resolve => {
    const routers = importAll(require.context('@', true, /^\.\/[\s\S]+\/router\/[\s\S]+\.js$/), 'dev-core', 'array')
    pageRouter[0].children = routers
    pageRouter[0].redirect = getRedirectPath(routers)
    router.addRoutes(pageRouter)
    resolve()
  })
}

const getRedirectPath = (routers) => {
  let route = routers[0]
  if (route.children && route.children.length > 0) {
    return ('/' + route.path + getRedirectPath(route.children))
  } else {
    return ('/' + route.path)
  }
}

export default filterRouter
